'use strict';

angular.module('BlurAdmin', [
  'ngAnimate',
  'ui.bootstrap',
  'ui.sortable',
  'ui.router',
  'ui.select',
  'ngSanitize',
  'ngEmoticons',
  'ngJsonExportExcel',
  'monospaced.qrcode',
  'angular-md5',
  'FBAngular',
  'uiCropper',
  'bootstrapLightbox',
  'emguo.poller',
  'ng.deviceDetector',
  'vjs.video',
  'ngTouch',
  'uuid',
  'jsTag',
  'angularMoment',
  'isteven-multi-select',
  'oitozero.ngSweetAlert',
  'ngLoadingSpinner',
  'checklist-model',
  'smart-table',
  "xeditable",
  'moment-picker',
  'angularQFileUpload',
  'mwl.calendar',
  'treeGrid',
  'angularValidator',
  'slick',
  'ui.slimscroll',
  'ngJsTree',
  'angular-progress-button-styles',

  'BlurAdmin.theme',
  'BlurAdmin.pages'
])
  .config(myConfig)
  .config(lightboxConfig)
  .run(myRun)
;

/** @ngInject */
function myConfig(momentPickerProvider, calendarConfig, $urlRouterProvider, $provide, $httpProvider, usSpinnerConfigProvider) {
  $urlRouterProvider
    .when('/', '/login')
    .when('', '/login')
    .otherwise('/404');

  // $httpProvider.defaults.withCredentials = true;
  $httpProvider.interceptors.push('myHttpInterceptor');
  $httpProvider.interceptors.push('myHttpHeader');

  usSpinnerConfigProvider.setTheme('bigBlue', {color: '#33ccff', radius: 10});

  momentPickerProvider.options({
    /* Picker properties */
    locale:        'zh-cn',
    format:        'YYYY-MM-DD',
    minView:       'decade',
    maxView:       'minute',
    startView:     'day',
    autoclose:     true,
    today:         true,
    keyboard:      true,

    /* Extra: Views properties */
    leftArrow:     '&larr;',
    rightArrow:    '&rarr;',
    yearsFormat:   'YYYY',
    monthsFormat:  'MMM',
    daysFormat:    'D',
    hoursFormat:   'HH:[00]',
    minutesFormat: moment.localeData().longDateFormat('LT'),
    secondsFormat: 'ss',
    minutesStep:   5,
    secondsStep:   1
  });


  calendarConfig.dateFormatter = 'moment'; // use moment to format dates
  calendarConfig.allDateFormats.moment.date.day = 'MMM Do';

  $provide.decorator('taOptions', ['taRegisterTool', '$delegate', function(taRegisterTool, taOptions) {
    taOptions.forceTextAngularSanitize = false;
    return taOptions;
  }]);

}

/** @ngInject */
function lightboxConfig(LightboxProvider) {
  LightboxProvider.templateUrl = 'app/pages/support/lightboxTemplate.html';
}

/** @ngInject */
function myRun($rootScope, localStorageService, loginService, $location, $state) {
  $(document).ready(function() {
    $(document).on('focus', ':input', function() {
      $(this).attr('autocomplete', 'off');
    });
  });

  $rootScope.$on('$stateChangeSuccess',
    function (event, toState, toParams, fromState, fromParams) {
      //$activityIndicator.stopAnimating();
      // layer.close($rootScope.ii);


    });

  $rootScope.$on('$stateChangeError',
    function (event, toState, toParams, fromState, fromParams, error) {
      // if (error.state) {
      //   $state.go('error');
      // }
      // if (error == "Not Authorized") {
      //   $state.go("notAuthorized");
      // }
    });

  $rootScope.$on('$stateChangeStart',
    function (event, toState, toParams, fromState, fromParams) {

      if (toState.name == 'login') return;

      if (localStorageService.cookie.get('user') == undefined || localStorageService.cookie.get('user').anonymous) {
        event.preventDefault();
        $state.go("login", {from: fromState.name});
      }

      // layer.close($rootScope.ii);
      // $rootScope.ii = layer.load(2);
    });

  loginService.initUser()
}